package nl.nhl.idp.Views.Charts;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Paint.Style;

/**
 * Set of DataPoints that can be added into one or more charts.
 * @author Alex Wijnbeek
 * 
 */
public class DataSet
{
	//font size for drawing legend and latest value
	private final int FONT_SIZE = 22;
	
	//Dataset values
	private ArrayList<DataPoint> data;
	
	//Paints
	private Paint graphPaint;
	private Paint textPaint;
	
	//properties
	private String name;
	/**
	 * Gets the DataSet's name
	 * @return
	 */
	public String GetName(){return name;}
	
	/**
	 * Gets the amount of DataPoints in this DataSet
	 * @return
	 */
	public int GetDataCount(){return data.size();}
	
	/**
	 * Gets the DataPoint at the specified index
	 * @param index
	 * @return
	 */
	public DataPoint GetPointAt(int index){return data.get(index);}
	
	/**
	 * Sets dataset color
	 * @param c integer representing and android.graphics.Color
	 */
	public void SetColor(int c)
	{
		graphPaint.setColor(c);
		textPaint.setColor(c);
	}
	
	/**
	 * Creates a new DataSet containing no DataPoints
	 * @param name The Name of this DataSet
	 */
	public DataSet(String name)
	{
		this(name, new ArrayList<DataPoint>());
	}
	
	/**
	 * Creates a new DataSet containing the given ArrayList of DataPoints
	 * @param name The Name of this DataSet
	 * @param data An ArrayList of DataPoints to add to the DataSet
	 */
	public DataSet(String name, ArrayList<DataPoint> data)
	{
		this.name = name;
		this.data = data;
		
		graphPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		graphPaint.setStrokeWidth(2);
		graphPaint.setColor(Color.GREEN);
		graphPaint.setStyle(Style.STROKE);
		
		textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		textPaint.setStrokeWidth(0);
		textPaint.setColor(Color.GREEN);
		textPaint.setStyle(Style.FILL);
		textPaint.setTextSize(FONT_SIZE);
	}
	
	/**
	 * Adds a DataPoint to the end of this DataSet
	 * @param point The DataPoint to add.
	 */
	public void Add(DataPoint point)
	{		
		data.add(point);
	}
	
	/**
	 * Adds a range of DataPoints to the end of the DataSet
	 * @param points an ArrayList of DataPoints to add
	 */
	public void AddRange(DataPoint points[])
	{
		for(int i=0;i<points.length;i++)
		{
			data.add(points[i]);
		}
	}
	
	/**
	 * Clears all datapoints from the dataset
	 */
	public void Clear()
	{
		data.clear();
	}
	
	/**
	 * Draws the DataSet in the given SensorChart.
	 * @param parent the SensorChart this DataSet is contained in.
	 * @param canvas The Canvas to draw on.
	 * @param index The index of this chart in it's parent
	 */
	public void Draw(SensorChart parent, Canvas canvas, int index)
	{
		//get ranges from given parent
		float maxX = parent.GetMaxX();
		float maxY = parent.GetMaxY();
		float minX = parent.GetMinX();
		float minY = parent.GetMinY();
		
		//get parent dimensions
		int width = parent.getWidth();
		int height = parent.getHeight();
		
		//firstpoint indicates whether we're drawing the first point in the graph
		boolean firstPoint = true;
		
		//create the graph path
		Path path = new Path();
		for(int i=0;i<data.size();i++)
		{
			//if the value we want to draw is in the current view
			if(data.get(i).GetX()+parent.GetScaleMinorX()>=maxX-parent.GetGraphRange())
			{
				//get screen coordinates from data coordinates
				float x = (data.get(i).GetX()-minX)/(maxX-minX)*width;
				float y = height-((data.get(i).GetY()-minY)/(maxY-minY)*height);
				
				if(firstPoint) //set first drawable point
				{
					firstPoint = false;
					path.moveTo(x, y);
				}
				else
					path.lineTo(x, y);
			
				//draw the value of the last point
				if(i == data.size()-1)
					canvas.drawText(data.get(i).GetY()+"", x-50, y, textPaint);
			}
		}
		
		//draw the graph path
		canvas.drawPath(path,  graphPaint);
		
		//draw the legend text for this dataset
		canvas.drawText(name, 25, 25+(FONT_SIZE*index), textPaint);
	}
}
